#pragma once  
#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;

namespace zyy 
{
    template<class T>
      class vector
      {
        public:
          typedef  T* iterator;
          typedef const T* const_iterator; 

          iterator bengin()
          {
            return _start;
          }

          iterator end()
          {
            return _finish;
          }


          const_iterator begin() const
        	{
        		return _start;
        	}
        
        	const_iterator end() const
        	{
        		return _finish;
        	}
        
          vector()
          {

          }


          vector(const vector<T>& v)
					{
						reserve(v.capacity());
						for (auto& e : v)
						{
							push_back(e);
						}
					}

          ~vector()
          {
            delete  [] _start;
            _start = _finish = _endofstorage = nullptr;
          }

          size_t size() const 
          {
              return _finish - _start;
          }


          T& operator[](size_t pos)
          {
              assert(pos < size());
              return  _start[pos];
          }



         const T& operator[](size_t pos) const
         {
             assert(pos < size());

             return _start[pos];
                                
         }
					
									
          size_t capcity()  const  
          {
            return _endofstorage - _start;
          }
					

					void reserve(size_t n)
          {
               
               if(n > capcity())
              {
                T* tmp=new T[n];//开辟新空间
                size_t old_size = size();
                memcpy(tmp,_start,size()*sizeof(T));//拷贝数据到就空间
                delete  [] _start;//释放就空间
                
                _start = tmp;
                _finish = tmp + old_size;
                _endofstorage = tmp + n;
              }

         }		

          void resize(size_t n,const T& val=T())///T() 无参的匿名对象
          {
            if(n>size())
            {
              reserve(n);
            //插入
            while(_finish < _start + n)
            {
              *_finish = val;
              ++_finish;
            }
            
            }
            else
            {
             // 删除
              _finish = _start + n;
            }
          }

          void push_back(const T& val)
          {
               if(_finish == _endofstorage)
              {
                reserve(capcity()==0?4:capcity()*2);
              }

              *_finish = val;
              ++_finish;
          }

          void pop_back()
          {
            assert(!empty());
             --_finish;
          }

          bool empty()
          {
            return _start == _finish;
          }

          void insert(iterator pos,const T&val)
          {
            assert(pos >= _start);
			      assert(pos <= _finish);

            if(_finish==_endofstorage)
            {
              size_t len = pos - _start;
              reserve(capcity()==0?4:capcity()*2);

              pos =_start +len;
            }

            iterator it = _finish -1;
            while(it>=size())
            {
              *(it + 1)=*it;
              --it;
            
            }
            *pos = val;
            ++_finish;
          }


          void erase(iterator pos)
          {
            assert(pos >= _start);
            assert(pos <= _finish);

            iterator it = pos + 1;
            while(it < _finish)
            {
              *(it - 1)=*it;
              ++it;
            }
            --_finish;
          }

        private:
          iterator _start=nullptr;//声明的时候给缺省值，初始化列表会自己取缺省值
          iterator _finish=nullptr;
          iterator _endofstorage=nullptr;
        };
        
        template<class T>
        void print_vector(const  vector<T> & v)
        {
          for(size_t i=0;i < v.size();i++)
          {
            cout  << v[i]<< " ";
          }
          cout <<endl;
        }




        void test_vector1()
        {
          vector<int>  v1;
          v1.push_back(1);
          v1.push_back(2);
          v1.push_back(3);
          v1.push_back(4);
          v1.push_back(5);
          print_vector(v1);

         vector<double> v2;
    		v2.push_back(1.1);
    		v2.push_back(2.2);
    		v2.push_back(3.1);
    		print_vector(v2);
    
    		v2.insert( v2.begin() , 11.11);
    		print_vector(v2);
    
    		v2.insert(v2.begin(), 11.11);
    		print_vector(v2);
    
    		v2.insert(v2.begin(), 11.11);
    		print_vector(v2);
    
    		v2.insert(v2.begin(), 11.11);
    		print_vector(v2);
    
    		v2.insert(v2.begin(), 11.11);
    		print_vector(v2);
    
    		v2.erase(v2.begin());
    		print_vector(v2);
    
    		v2.erase(v2.begin()+4);
    		print_vector(v2);
    
         // for(size_t i = 0;i < v.size();i++)
         // {
         //   cout << v[i] << " ";
         // }
         // cout << endl;


         // vector<int>::iterator it = v.bengin();
         // while(it != v.end())
         // {
         //   cout<< *it << " ";
         //   ++it;
         // }
         // cout<<endl;
        }

}
